//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by mac on 15/7/29.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        

    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        setupComposedButton()
        
    }

    
    //添加所有子控制器
    func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    //添加子控制器
    func addChildViewController(vc:UIViewController,title:String,imageName:String){
       
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
            }
    
    //    MARK: ----------------------------懒加载---撰写按钮
  lazy private var composeButton:UIButton = {
            let btn = UIButton()
            btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState:UIControlState.Normal)
            btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
            btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
            btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
            self.tabBar.addSubview(btn)
    btn.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
            return btn
    }()
    
    /// 设置撰写按钮位置
    private func setupComposedButton() {
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        composeButton.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
    
    func clickComposeButton(){
        
        print(__FUNCTION__)
    }
   

  
}
